﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using MySql.Data.MySqlClient;

namespace DBWorker
{
    /// <summary>
    /// class, that works with MySQL base
    /// </summary>
    public class DBWorkerMySQL: DBWokerBase //, IDBWorker
    {
        #region Constructors and fields

        public DBWorkerMySQL()
            : base()
        {

        }

        public DBWorkerMySQL(string server, string databaseName)
            :base(server, databaseName)
        {

        }

        public bool AllowZeroDateTime
        {
            set;
            get;
        }

        #endregion Constructors and fields

        #region Base using methods

        public override string GetConnectionString()
        {
            return "SERVER=" + Server + ";" +
                "PORT=" + Port + ";" +
                "DATABASE=" + DatabaseName + ";" +
                "UID=" + UserID + ";" +
                "PWD=" + Password; 
               // "Allow Zero Datetime = " + AllowZeroDateTime.ToString() + ";";
            //return base.GetConnectionString();
        }

        public DataTable GetDataTableByTableName(string tableName)
        {
            string query = "SELECT * FROM " + tableName;
            return this.GetDataTableByCommand(query);
        }

        public DataTable GetDataTableByCommand(string query)
        {
            //MySqlConnection mysqlConnection = new MySqlConnection(GetConnectionString());
            //MySqlCommand mysqlCommand = new MySqlCommand(@query, mysqlConnection);
            return base.GetDataTableByCommand(this.GetMySQLCommandByQueryString(query));
        }

        public DataTable GetDataTableByCommand(MySqlCommand command)
        {
            //base.GetDataTableByCommand(command);
            return base.GetDataTableByCommand(command);
        }

        public DataRow GetDataRowByCommand(string query)
        {
            //MySqlConnection mysqlConnection = new MySqlConnection(GetConnectionString());
            //MySqlCommand mysqlCommand = new MySqlCommand(@query, mysqlConnection);
            return base.GetDataRowByCommand(this.GetMySQLCommandByQueryString(query));
        }

        public DataRow GetDataRowByCommand(MySqlCommand command)
        {
            return base.GetDataRowByCommand(command);
        }

        public object GetObjectByCommand(string query)
        {
            return base.GetObjectByCommand(this.GetMySQLCommandByQueryString(query));
        }

        public object GetObjectByCommand(MySqlCommand command)
        {
            return base.GetObjectByCommand(command);
        }

        public void ExecuteScalar(string query)
        {
            base.ExecuteScalar(this.GetMySQLCommandByQueryString(query));
        }
        
        public void ExecuteScalar(MySqlCommand command)
        {
            base.ExecuteScalar(command);
        }

        public DataTable GetDataTableSchema(string query)
        {
            return base.GetDataTableSchema(this.GetMySQLCommandByQueryString(query));
        }

        public DataTable GetDataTableSchema(MySqlCommand command)
        {
            return base.GetDataTableSchema(command);
        }

        #endregion Base using methods

        #region Private methods
        MySqlCommand GetMySQLCommandByQueryString(string query)
        {
            MySqlConnection mysqlConnection = new MySqlConnection(GetConnectionString());
            MySqlCommand mysqlCommand = new MySqlCommand(@query, mysqlConnection);
            return mysqlCommand;
        }


        #endregion Private methods

    }
}
